<template>
  <div class="image-map">
    <div class="image">
      <img @click="changeVisible" :src="modelValue" />
    </div>
    <el-popover
      placement="right"
      :offset="120"
      :show-arrow="false"
      popper-class="image-map_popover"
      effect="dark"
      width="264"
      trigger="click"
      ref="popoverRef"
      :visible="visible"
    >
      <template #reference>
        <el-button v-if="isShowButton" type="primary" @click="changeVisible">{{
          buttonText
        }}</el-button>
      </template>
      <template #default>
        <div class="title">
          <span>{{ title }}</span>
          <img
            class="close"
            v-if="showClose"
            @click="changeVisible"
            src="@/assets/png/close.png"
            alt=""
          />
          <UploadImage :type="props.type" @success="search" v-else />
        </div>
        <div class="tabs" v-if="tabs?.length > 0">
          <div class="tabs-container">
            <div
              class="tab"
              v-for="item in tabs"
              :key="item.value"
              :class="item.value === feature ? 'active' : ''"
              @click="changeTab(item.value!)"
            >
              {{ item.label }}
            </div>
          </div>
          <UploadImage
            :type="props.type"
            @success="search"
            v-if="tabs.length > 0 && feature === FeatureEnum.PUBLIC"
          />
        </div>
        <div class="list" v-loading="loading">
          <div
            class="item"
            :class="activeUrl === item.address ? 'active' : ''"
            v-for="item of fileList"
            :key="item.receiveId"
            @click="handleSelect(item)"
          >
            <img :title="item.name" :src="item.address" />
            <div class="remove" v-if="feature === FeatureEnum.PUBLIC">
              <img
                class="close"
                @click="handleRemove(item.receiveId)"
                src="@/assets/png/ic_remove.png"
              />
            </div>
          </div>
        </div>
        <div class="footer">
          <div class="pagination">
            <template v-if="countPage > 1">
              <img
                src="@/assets/png/ic_arrow_left.png"
                width="12"
                height="12"
                alt=""
                @click="handlePrev"
              />
              <div class="pagination_option">
                <div class="input">
                  <el-input
                    v-model="pageInfo.pageNum"
                    @blur="getList"
                    @keyup.enter="$event.target.blur()"
                  ></el-input>
                </div>
                <div class="divider">/</div>
                <div class="countpage">{{ countPage }}</div>
              </div>
              <img
                src="@/assets/png/ic_arrow_right.png"
                width="12"
                height="12"
                alt=""
                @click="handleNext"
              />
            </template>
          </div>
          <div class="btns">
            <el-button class="default" size="small" @click="changeVisible">取消</el-button>
            <el-button size="small" type="primary" @click="handleOk">确定</el-button>
          </div>
        </div>
      </template>
    </el-popover>
  </div>
</template>
<script lang="ts" setup>
import { ref, toRefs } from 'vue';
import { Option, FeatureEnum } from '../../index.typing';
import { getFileList, deleteFile } from '@/api/common';
import { Files } from '@/api/common/types';
import UploadImage from '../upload-image/index.vue';
import { ElMessageBox } from 'element-plus';
import { getResPath } from '@/utils/cimup';

interface IProps {
  isShowButton?: boolean;
  title: string;
  showClose?: boolean;
  tabs?: Option[] | any;
  modelValue: string;
  buttonText?: string;
  type: string; //  [transparent:透明; reflection:反射; sticker:贴图]
  disabled?: boolean;
}

const props = withDefaults(defineProps<IProps>(), {
  isShowButton: true,
  buttonText: '替换样式',
  type: 'sticker',
  disalbed: false,
});
const { isShowButton, title, tabs, type, modelValue } = toRefs(props);
const pageInfo = reactive({ pageSize: 16, pageNum: 1 });
const fileList = ref<Files.Item[]>([]);
const activeUrl = ref('');
const count = ref(0);
const visible = ref(false);
const feature = ref('public');
const loading = ref(false);
const emits = defineEmits<{ (e: 'update:modelValue', value: string): void }>();

const changeTab = (newTab: string) => {
  if (feature.value !== newTab) {
    feature.value = newTab;
    search();
  }
};
const changeVisible = () => {
  if (props.disabled) {
    return;
  }
  visible.value = !visible.value;
};
const handleRemove = async (receiveId: string) => {
  await ElMessageBox.confirm('请确定要删除吗？', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  });
  await deleteFile({ receiveId });
  search();
};
const countPage = computed(() => {
  return Math.ceil(count.value / pageInfo.pageSize);
});
const search = () => {
  pageInfo.pageNum = 1;
  getList();
};
const getList = async () => {
  loading.value = true;
  const res = await getFileList(pageInfo, { type: type.value, feature: feature.value }).finally(
    () => (loading.value = false)
  );
  fileList.value = res.list?.map((x) => {
    return { ...x, address: getResPath(x.address) };
  });
  count.value = res.count;
};
const handleSelect = (item: Files.Item) => {
  activeUrl.value = item.address;
};
const handleOk = () => {
  emits('update:modelValue', activeUrl.value);
  changeVisible();
};
const handlePrev = () => {
  if (pageInfo.pageNum !== 1) {
    --pageInfo.pageNum;
    getList();
  }
};
const handleNext = () => {
  if (fileList.value?.length !== count.value) {
    ++pageInfo.pageNum;
    getList();
  }
};
onMounted(() => search());
</script>
<style lang="less" scoped>
.image-map {
  display: flex;
  align-items: center;
  .image {
    background: #303030;
    border-radius: 3px;
    border: 1px solid #464646;
    padding: 12px;
    margin-right: 11px;
    img {
      width: 46px;
      height: 46px;
    }
  }
}
.image-map_popover {
  background: #303030;
  border: 1px solid #464646;
  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 12px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    color: #ffffff;
    .close {
      cursor: pointer;
    }
  }
  .tabs {
    background: #3d3d3d;
    border-radius: 4px;
    display: flex;
    margin-top: 10px;
    padding: 4px 6px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .tabs-container {
      flex: 1;
      display: inline-flex;
      .tab {
        font-size: 12px;
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        color: #ffffff;
        cursor: pointer;
        & + .tab {
          margin-left: 16px;
        }
        &.active {
          font-size: 12px;
          font-family: PingFangSC, PingFang SC;
          font-weight: 500;
          color: #0560fd;
          position: relative;
          border-bottom: 2px solid #0560fd;
        }
      }
    }
  }
  .footer {
    margin-top: 14px;
    display: flex;
    .btns,
    .pagination {
      flex: 1;
      display: flex;
      align-items: center;
      img {
        cursor: pointer;
      }
      .pagination_option {
        padding: 0 5px;
        display: flex;
        align-items: center;
        font-size: 14px;
        font-family: NunitoSans, NunitoSans;
        font-weight: 400;
        color: #e0e0e0;
        .input {
          width: 22px;
          height: 22px;
          ::v-deep(.el-input) {
            padding: 0;
            font-size: 14px;
            .el-input__wrapper {
              padding: 0;
              height: 22px;
              background: #3d3d3d;
              box-shadow: none;
              border: none;
              .el-input__inner {
                text-align: center;
                color: #e0e0e0;
              }
            }
          }
        }
        .divider {
          padding: 0 16px 0 6px;
        }
      }
    }
    .btns {
      .el-button.default {
        background: transparent;
        border-radius: 4px;
        border: 1px solid #464646;
        font-size: 12px;
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        color: #ffffff;
      }
    }
  }
  .list {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 14px;
    margin-top: 14px;
    .item {
      width: 48px;
      height: 48px;
      border-radius: 4px;
      position: relative;
      &.active {
        border: 1px solid #0560fd;
      }
      img {
        width: 100%;
        height: 100%;
        border-radius: 4px;
      }
      .remove {
        background: #0560fd;
        border-radius: 0px 0px 2px 2px;
        opacity: 0.68;
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        text-align: center;
        display: none;
        img {
          width: 10px;
          height: 10px;
        }
      }
      &:hover {
        .remove {
          display: block;
        }
      }
    }
  }
}
.el-upload-list {
  display: none;
}
</style>
